package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyComment;
import cn.wolfcode.trip.base.domain.DailyType;
import cn.wolfcode.trip.base.mapper.DailyCommentMapper;
import cn.wolfcode.trip.base.mapper.DailyMapper;
import cn.wolfcode.trip.base.mapper.DailyTypeMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class DailyServiceImpl implements IDailyService {

    @Autowired
    private DailyMapper dailyMapper;
    @Autowired
    private DailyTypeMapper dailyTypeMapper;
    @Autowired
    private DailyCommentMapper commentMapper;


    @Override
    public PageInfo<Daily> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Daily> list=dailyMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Daily get(Long id) {
        return dailyMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<DailyType> getDailyType(){
        List<DailyType> dailyTypes = dailyTypeMapper.selectAll();
        for (DailyType dailyType : dailyTypes) {
            List<Daily> dailies = dailyMapper.getDailyType(dailyType.getId());
            dailyType.setDailies(dailies);
        }
        return dailyTypes;
    }

    @Override
    public List<DailyComment> getComments(Long dailyId) {

        return commentMapper.selectAllByDailyId(dailyId);
    }

    @Override
    public void saveComments(DailyComment dailyComment) {
        dailyComment.setCreateTime(new Date());
        if(dailyComment.getId()==null){
            commentMapper.insert(dailyComment);
        }else {
            DailyComment com = commentMapper.selectByPrimaryKey(dailyComment.getId());
            if(com.getPraise()==0){
                dailyComment.setPraise(1);

            }else {
                dailyComment.setPraise(0);
            }
            commentMapper.updateByPrimaryKey(dailyComment);
        }

    }
}
